import axios from '../plugins/axios.js';
import { defineStore } from 'pinia';
import { handleCommonRes } from '../utils/resolver.js';
import { useAutoAction, useAutoGetter } from './autoActionGetter.js';
import { getStoreId } from './utils.js';

const id = getStoreId('publicData');
export const usePublicDataStore = defineStore(id, {
  state: () => ({
    yardListStore: [],
  }),
  getters: {
    getYardList: useAutoGetter({
      getter: (state) => (area_code) => state.yardListStore[area_code] || [],
      action: function (params) {
        this.getYardListAsync(params);
      },
    }),
  },
  actions: {
    getYardListAsync: useAutoAction({
      action: async function (area_code) {
        const getYardListByAreaCode = (area_code) => {
          return axios.get(`/xw-microservice/xw-actual-data/v3/house/yard/select/area_code`, {
            params: { area_code: area_code },
          });
        };
        const data = await handleCommonRes(getYardListByAreaCode, area_code);
        return (this.yardListStore[area_code] = data.map((item) => ({
          label: item.yard_name,
          value: item.yard_id,
        })));
      },
    }),
  },
});
